/**
 * 全局页面初始化
 */

function wxRequest(method, url, param, successCallBack, CallBackError, completeCallBack) {
  wx.request({
    url: url,
    method: method,
    data: param,
    header: {
      'X_ManuName': getApp().globalData.manuName
    },
    success: function (res) {
      if (res.statusCode == 200) {
        var data = res.data;
        successCallBack(data)
        console.log(res.data)
      }
    },
    fail: function (res) {
      if (typeof CallBackError == "function") {
        CallBackError(res.data)
      }
    },
    complete: function (res) {
      if (typeof completeCallBack == "function") {
        completeCallBack(res.data);
      }
    }
  })
}



/**
 * 微信navigateTo
 */
function navigateTo(url) {
  var index = url.indexOf("?");
  var param = "";
  if (index > -1) {
    param = url.substring(index);
    url = url.substring(0, index);
  }
  GetCustomizView(url, function (data) {
    if (data.code == 1200 && data.content && data.content.View) {
      wx.navigateTo({
        url: data.content.View + param,
      })
      return;
    }
    wx.navigateTo({
      url: url + param
    })
  }, function (data) {
    wx.navigateTo({
      url: url + param
    })
  })
}

/**
 * 微信redirectTo
 */
function redirectTo(url) {
  var index = url.indexOf("?");
  var param = "";
  if (index > -1) {
    param = url.substring(index);
    url = url.substring(0, index);
  }
  GetCustomizView(url, function (data) {
    if (data.code == 1200 && data.content && data.content.View) {
      wx.redirectTo({
        url: data.content.View + param,
      })
      return;
    }
    wx.redirectTo({
      url: url + param
    })
  }, function (data) {
    wx.redirectTo({
      url: url + param
    })
  })
}

/**
 * 微信switchTab
 */
function switchTab(url) {
  var index = url.indexOf("?");
  var param = "";
  if (index > -1) {
    param = url.substring(index);
    url = url.substring(0, index);
  }
  GetCustomizView(url, function (data) {
    if (data.code == 1200 && data.content && data.content.View) {
      wx.switchTab({
        url: data.content.View + param,
      })
      return;
    }
    wx.switchTab({
      url: url + param
    })
  }, function (data) {
    wx.switchTab({
      url: url + param
    })
  })
}




function GetCustomizView(mapUrl, successCallBack, CallBackError) {
  var globalData = getApp().globalData;
  var manuName = globalData.manuName;//厂家名
  wxRequest("get", globalData.apiurl + "/member/api/Customized/GetCustomizView", { manuName: manuName, map: mapUrl }, successCallBack, CallBackError);
}

function pageInit(CallBack) {
  const app = getApp();
  var options = getParam();
  console.log(options);
  console.log(getCurrentPageUrlWithArgs())
  var scene = decodeURIComponent(options.scene);
  if (scene.indexOf("UserID") > -1) {
    scene = scene.replace("UserID", "");
  } else {
    scene =null;
  }
  if (wx.getStorageSync('UserInfo')) {
    if (wx.getStorageSync("IsBindMobile")) {
      wx.redirectTo({
        url: '/pages/register/register?pageUrl=' + encodeURIComponent(getCurrentPageUrlWithArgs()),
      })
    }
    else if (scene) //请求api里面有没有上下级关系吧
    {
      wx.request({
        url: app.globalData.apiurl + '/member/api/Register/UpdateUserRelation',
        method: 'get',
        header: {
          'X_ManuName': app.globalData.manuName
        },
        data: {
          CustId: app.globalData.custID,
          recommendID: scene,
          userId:wx.getStorageSync('UserInfo').UserID
        }, success: res => {
          var data = res.data;
          if (data.code == 1200 && data.content) {
            console.log(data.content)
          }
        },complete:res=>{
          console.log("1121")
          CallBack();
        }
      });

    } else {
      CallBack();
    }

  }
  else {
    wx.redirectTo({
      url: '/pages/sign/sign?parentId=' + scene + '&pageUrl=' + encodeURIComponent(getCurrentPageUrlWithArgs()),
    })
  }

}
/*获取当前页带参数的url*/
function getCurrentPageUrlWithArgs() {
  var pages = getCurrentPages() //获取加载的页面
  var currentPage = pages[pages.length - 1] //获取当前页面的对象
  var url = "/" + currentPage.route //当前页面url
  var options = currentPage.options //如果要获取url中所带的参数可以查看options
  //拼接url的参数
  var urlWithArgs = url + '?'
  for (var key in options) {
    if (key != "scene") {
      var value = options[key]
      urlWithArgs += key + '=' + value + '&'
    }
  }
  urlWithArgs = urlWithArgs.substring(0, urlWithArgs.length - 1)

  return urlWithArgs
}

/**
 * 获取当前页面参数
 */
function getParam() {
  var pages = getCurrentPages() //获取加载的页面
  var currentPage = pages[pages.length - 1] //获取当前页面的对象
  var url = currentPage.route //当前页面url
  var options = currentPage.options //如果要获取url中所带的参数可以查看options
  return options;
}
module.exports = {
  pageInit: pageInit,
  wxRequest: wxRequest,
  navigateTo: navigateTo,
  switchTab: switchTab,
  redirectTo: redirectTo,
  getCurrentPageUrlWithArgs: getCurrentPageUrlWithArgs
}